// 邀请函页面功能
document.addEventListener('DOMContentLoaded', function() {
    // 加载数据
    loadJSON('data/content.json').then(data => {
        if (data && data.party) {
            // 填充邀请函内容
            fillInvitationContent(data.party);
            
            // 初始化信封交互
            initEnvelopeInteraction();
            
            // 初始化找帽子游戏
            initHatGame();
        }
    });
});

// 填充邀请函内容
function fillInvitationContent(partyData) {
    document.getElementById('party-date').textContent = partyData.date;
    document.getElementById('party-location').textContent = partyData.location;
    document.getElementById('party-address').textContent = partyData.address;
    document.getElementById('party-contact').textContent = partyData.contact;
    
    // 设置地图图片
    const mapImage = document.getElementById('map-image');
    if (mapImage && partyData.mapImage) {
        mapImage.src = partyData.mapImage;
    }
}

// 初始化信封交互
function initEnvelopeInteraction() {
    const envelope = document.getElementById('envelope');
    const openButton = document.getElementById('open-envelope');
    
    if (envelope && openButton) {
        openButton.addEventListener('click', function() {
            // 播放打开音效
            playSound('audio/envelope_open.mp3');
            
            // 打开信封
            envelope.classList.add('opened');
            
            // 隐藏打开按钮
            this.style.display = 'none';
        });
    }
}

// 初始化找帽子游戏
function initHatGame() {
    const gameArea = document.getElementById('game-area');
    const hatsFoundElement = document.getElementById('hats-found');
    const totalHatsElement = document.getElementById('total-hats');
    const rsvpButton = document.getElementById('rsvp-button');
    
    if (!gameArea || !hatsFoundElement || !totalHatsElement) return;
    
    // 游戏状态
    const totalHats = 5;
    let hatsFound = 0;
    let gameCompleted = false;
    
    // 更新游戏状态显示
    function updateGameStatus() {
        hatsFoundElement.textContent = hatsFound;
        totalHatsElement.textContent = totalHats;
        
        // 游戏完成
        if (hatsFound === totalHats && !gameCompleted) {
            gameCompleted = true;
            showToast('恭喜你找到所有生日帽！');
            playSound('audio/game_complete.mp3');
            
            // 创建礼花效果
            createConfetti(200, 'game-area');
            
            // 解锁惊喜页面
            localStorage.setItem('surpriseUnlocked', 'true');
            
            // 显示RSVP按钮
            if (rsvpButton) {
                rsvpButton.style.display = 'inline-flex';
            }
        }
    }
    
    // 创建生日帽
    function createBirthdayHats() {
        for (let i = 0; i < totalHats; i++) {
            const hat = document.createElement('div');
            hat.className = 'birthday-hat';
            hat.innerHTML = '<i class="fa fa-hat-wizard text-2xl text-accent"></i>';
            
            // 随机位置
            const left = Math.random() * 80 + 10; // 10% - 90%
            const top = Math.random() * 80 + 10; // 10% - 90%
            
            hat.style.left = `${left}%`;
            hat.style.top = `${top}%`;
            
            // 随机动画延迟
            hat.style.animationDelay = `${Math.random() * 3}s`;
            
            // 点击事件
            hat.addEventListener('click', function() {
                if (!this.classList.contains('found')) {
                    this.classList.add('found');
                    this.innerHTML = '<i class="fa fa-check-circle text-2xl text-green"></i>';
                    this.style.transform = 'scale(1.5)';
                    
                    hatsFound++;
                    updateGameStatus();
                    playSound('audio/find_hat.mp3');
                    
                    // 移除点击事件
                    this.removeEventListener('click', arguments.callee);
                    
                    // 恢复大小
                    setTimeout(() => {
                        this.style.transform = 'scale(0)';
                        setTimeout(() => this.remove(), 300);
                    }, 1000);
                }
            });
            
            gameArea.appendChild(hat);
            
            // 延迟显示，营造随机出现的效果
            setTimeout(() => {
                hat.classList.add('visible');
                hat.style.animation = 'hat-float 3s ease-in-out infinite';
            }, i * 1000 + Math.random() * 2000);
        }
    }
    
    // 初始化游戏状态
    updateGameStatus();
    
    // 创建生日帽
    setTimeout(createBirthdayHats, 1000);
    
    // RSVP按钮事件
    if (rsvpButton) {
        rsvpButton.addEventListener('click', function() {
            // 播放礼花音效
            playSound('audio/celebration.mp3');
            
            // 创建礼花效果
            createConfetti(300);
            
            // 显示成功提示
            showToast('感谢您的参与，我们期待您的到来！');
        });
    }
}

// 播放音效
function playSound(url) {
    const audio = new Audio(url);
    audio.volume = 0.5;
    audio.play().catch(e => console.log('播放音效失败:', e));
}
